import requset from "@/utils/request"
import { setToken, clearToken } from "@/utils/storage"
import { LOGIN, LOGOUT } from "../constants"

export const login = (payload) => {
  return async (dispatch) => {
    const res = await requset({
      method: "post",
      url: "/authorizations",
      data: payload,
    })
    const token = res.data.data.token
    // 存储到本地
    setToken(token)
    // redux存储
    dispatch({
      type: LOGIN,
      payload: token,
    })
  }
}
export const logout = () => {
  return (dispatch) => {
    clearToken()
    dispatch({
      type: LOGOUT,
    })
  }
}
